package com.ibangnuo.manage.service.system;

import com.alibaba.fastjson.JSONObject;
import com.ibangnuo.framework.domain.system.SysConfig;
import com.ibangnuo.framework.model.response.*;
import com.ibangnuo.framework.redisKey.Rediskey;
import com.ibangnuo.manage.dao.system.SysConfigRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: wwy
 * @Date: 2019-12-21 14:28
 */
@Service
public class SySConfigService {
    private static Logger LOGGER = LoggerFactory.getLogger(SySConfigService.class);
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private ValueOperations valueOperations;
    @Autowired
    private SysConfigRepository sysConfigRepository;

    /**
     * 根据type查找
     * @param type
     * @return
     */
    public ObjectResult findByType(String type,String snNum){
        try {
            String keyId = Rediskey.SYSCONFIG_TYPE+type+":"+snNum;
            String jsonStr = (String)valueOperations.get(keyId);
            if(StringUtils.isEmpty(jsonStr)){
                SysConfig sysConfig = sysConfigRepository.findByTypeAndSnNum(type,snNum);
                if(sysConfig==null){
                    return new ObjectResult(CommonCode.FAIL,null);
                }
                jsonStr = JSONObject.toJSONString(sysConfig);
                valueOperations.set(keyId,jsonStr);
            }
            SysConfig sysConfig = JSONObject.parseObject(jsonStr, SysConfig.class);
            return new ObjectResult(CommonCode.SUCCESS,sysConfig);
        } catch (Exception e) {
            LOGGER.info("缓存库连接异常:{}",e.getMessage());
            SysConfig sysConfig = sysConfigRepository.findByTypeAndSnNum(type,snNum);
            return new ObjectResult(CommonCode.SUCCESS,sysConfig);
        }
    }

    /**
     * 添加
     * @param sysConfig
     * @return
     */
    public ResponseResult add(SysConfig sysConfig){
        sysConfigRepository.save(sysConfig);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    /**
     * 更新
     * 1.判断是否能查询出来，
     * 2.查询不出来进行添加操作
     * 3.查询出来进行更新操作
     * @param id
     * @param sysConfig
     * @return
     */
    public ResponseResult update(String id,SysConfig sysConfig){
        SysConfig one = this.getById(id);
        if(one==null){
            one = new SysConfig();
        }
        one.setModel(sysConfig.getModel());
        one.setName(sysConfig.getName());
        one.setType(sysConfig.getType());
        one.setSnNum(sysConfig.getSnNum());
        one.setCreateDate(new Date());
        SysConfig save = sysConfigRepository.save(one);
        String keyTypeId = Rediskey.SYSCONFIG_TYPE+save.getType()+":"+save.getSnNum();
        redisTemplate.delete(keyTypeId);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    public ResponseResult delete(String id){
        SysConfig one = this.getById(id);
        String keyTypeId = Rediskey.SYSCONFIG_TYPE+one.getType()+":"+one.getSnNum();
        sysConfigRepository.deleteById(id);
        redisTemplate.delete(keyTypeId);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    private SysConfig getById(String id){
        Optional<SysConfig> optional = sysConfigRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }


    /**
     * 根据id查询全部
     * @return
     */
    public QueryResponseResult findAll(){
        List<SysConfig> all = sysConfigRepository.findAll();
        QueryResult queryResult = new QueryResult();
        queryResult.setList(all);
        queryResult.setTotal(all.size());
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }
}
